package com.joker.web.controller.orange;

import java.util.List;
import java.util.Arrays;

import com.joker.orange.domain.vo.AgentUserVo;
import com.joker.orange.service.IAgentUserService;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.joker.common.annotation.RepeatSubmit;
import com.joker.common.annotation.Log;
import com.joker.common.core.controller.BaseController;
import com.joker.common.core.domain.PageQuery;
import com.joker.common.core.domain.R;
import com.joker.common.core.validate.AddGroup;
import com.joker.common.core.validate.EditGroup;
import com.joker.common.enums.BusinessType;
import com.joker.common.utils.poi.ExcelUtil;
import com.joker.orange.domain.vo.UserDeductionVo;
import com.joker.orange.domain.bo.UserDeductionBo;
import com.joker.orange.service.IUserDeductionService;
import com.joker.common.core.page.TableDataInfo;

/**
 * 用户扣单配置
 *
 * @author Joker
 * @date 2024-07-15
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/deduction/config")
public class UserDeductionController extends BaseController {

    private final IUserDeductionService iUserDeductionService;
    private final IAgentUserService agentUserService;

    /**
     * 查询用户扣单配置列表
     */
    @SaCheckPermission("deduction:config:list")
    @GetMapping("/list")
    public TableDataInfo<UserDeductionVo> list(UserDeductionBo bo, PageQuery pageQuery) {
        TableDataInfo<UserDeductionVo> tableDataInfo = iUserDeductionService.queryPageList(bo, pageQuery);
        for (UserDeductionVo row : tableDataInfo.getRows()) {
            AgentUserVo agentUserVo = agentUserService.queryById(row.getUserId());
            row.setNickName(agentUserVo.getNickName());
        }
        return tableDataInfo;
    }

    /**
     * 导出用户扣单配置列表
     */
    @SaCheckPermission("deduction:config:export")
    @Log(title = "用户扣单配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(UserDeductionBo bo, HttpServletResponse response) {
        List<UserDeductionVo> list = iUserDeductionService.queryList(bo);
        ExcelUtil.exportExcel(list, "用户扣单配置", UserDeductionVo.class, response);
    }

    /**
     * 获取用户扣单配置详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("deduction:config:query")
    @GetMapping("/{id}")
    public R<UserDeductionVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iUserDeductionService.queryById(id));
    }

    /**
     * 新增用户扣单配置
     */
    @SaCheckPermission("deduction:config:add")
    @Log(title = "用户扣单配置", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody UserDeductionBo bo) {
        AgentUserVo agentUserVo = agentUserService.queryById(bo.getUserId());
        if ("1".equals(agentUserVo.getAgentLevel())) {
            bo.setDeductionType("1");
        } else if ("2".equals(agentUserVo.getAgentLevel())) {
            bo.setDeductionType("2");
        } else {
            bo.setDeductionType("3");
        }
        return toAjax(iUserDeductionService.insertByBo(bo));
    }

    /**
     * 修改用户扣单配置
     */
    @SaCheckPermission("deduction:config:edit")
    @Log(title = "用户扣单配置", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody UserDeductionBo bo) {
        return toAjax(iUserDeductionService.updateByBo(bo));
    }

    /**
     * 删除用户扣单配置
     *
     * @param ids 主键串
     */
    @SaCheckPermission("deduction:config:remove")
    @Log(title = "用户扣单配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iUserDeductionService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
